<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="关联项目" prop="inviteId">
						<el-select v-model="form.inviteId" clearable filterable remote :remote-method="remoteMethod" style="width: 100%" placeholder="项目名称、编号">
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="标书类型" prop="fileType">
						<sc-select v-model="form.fileType" :params="{code:'fileType'}" :apiObj="$API.invite.inviteDic.getInviteDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="编制部门" prop="editDept">
						<select-dept-dialog v-model:deptId="form.editDept" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="编制时间" prop="editTime">
						<el-date-picker v-model="form.editTime" placeholder="编制时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="文件版本" prop="version">
						<el-input v-model="form.version" placeholder="文件版本" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="是否可出售" prop="status">
						<el-radio-group v-model="form.status">
							<el-radio-button label="0">禁售</el-radio-button>
							<el-radio-button label="1">可售</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="标书价格" prop="price">
						<el-input v-model="form.price" placeholder="出售价格" clearable type="number">
							<template #append>
								元
							</template>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="可出售数量" prop="quantity">
						<el-input v-model="form.quantity" placeholder="可出售数量" clearable type="number">
							<template #append>
								份
							</template>
						</el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" placeholder="备注说明" clearable maxlength="380" type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="标书文件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'invite'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectDeptDialog from "@/views/module/selectDeptDialog.vue";
import AttachMenu from "@/views/module/attachMenu.vue";

export default {
	emits: ['success', 'closed'],
	components: {
		AttachMenu,
		selectDeptDialog
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			titleMap: {
				add: '创建标书文件',
				edit: '编辑标书文件',
			},
			templates: [],
			options:[],
			apiObj: this.$API.file.attach.uploadFile,
			fileList: [],
			form: {
				fileId:'',
				inviteId:'',
				editDept:'',
				editTime:'',
				fileType:'',
				version: '',
				status:'0',
				price:0.0,
				quantity:0,
				remark:'',
				attachId:''
			},
			rules: {
				inviteId: [
					{required: true, message: '关联招标项目不能为空', trigger: 'blur'}
				],
				fileType: [
					{required: true, message: '标书类型不能为空', trigger: 'blur'}
				],
				editDept: [
					{required: true, message: '编制部门不能为空', trigger: 'blur'}
				],
				editTime: [
					{required: true, message: '编制时间不能为空', trigger: 'blur'}
				],
				version: [
					{required: true, message: '标书版本不能为空', trigger: 'blur'}
				],
			}
		}
	},
	mounted() {
		this.remoteMethod("");
	},
	methods: {
		async remoteMethod(query){
			let res = await this.$API.invite.inviteRecord.getInviteRecordListForSelect.get({keyword:query});
			if (res.code == 200) {
				this.options = res.data
			}
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.invite.inviteFile.insertInviteFile.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.invite.inviteFile.updateInviteFile.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.fileId = data.fileId
			this.form.inviteId = data.inviteId
			this.form.editDept = data.editDept
			this.form.editTime = data.editTime
			this.form.fileType = data.fileType
			this.form.version = data.version
			this.form.status = data.status
			this.form.price = data.price
			this.form.quantity = data.quantity
			this.form.attachId = data.attachId
			this.form.remark = data.remark
		},
		addNewFile(res, file)
		{
			let tArr =[];
			if(this.form.attachId!=undefined&&this.form.attachId!=null&&this.form.attachId!=''){
				tArr = this.form.attachId.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
